/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kitaplaner.application;

import java.sql.Connection;
import java.util.ArrayList;
import kitaplaner.database.Database;
import kitaplaner.database.Kita;
import kitaplaner.database.Status;

/**
 *
 * @author kristina
 */
public class StatusApp {
    Kita kita = new Kita();
    Connection conn;
    Database db = new Database();

    public StatusApp(Kita kita, Connection conn)
    {
        this.conn = conn;
        this.kita = kita;
    }

    public Object [] createCompoboxText()
    {
        Object [] comboItems;

        ArrayList <Status> status = kita.getStatus();
        ArrayList <String> statusNamens = new ArrayList<String>();

        for (int i = 0; i < status.size(); i++)
        {
            statusNamens.add(status.get(i).getName());
        }
        comboItems = statusNamens.toArray();

        return comboItems;
    }

    public Status getStatusById(int id)
    {
        Status st = null;
        ArrayList <Status> alSt = kita.getStatus();

        for (int i = 0; i < alSt.size(); i++)
        {
            if (alSt.get(i).getId() == id)
            st = alSt.get(i);
        }

        return st;
    }

    public Status getStatusByName(String name)
    {
        Status st = null;
        ArrayList <Status> alSt = kita.getStatus();

        for (int i = 0; i < alSt.size(); i++)
        {
            if (alSt.get(i).getName().equals(name))
                st = alSt.get(i);
        }

        return st;
    }

    public Boolean deleteStatus(int id)
    {
        Boolean success = false;
        ArrayList <Status> alStat = kita.getStatus();

        for (int i = 0; i < alStat.size(); i++)
        {
            if (alStat.get(i).getId() == id)
            {
                alStat.remove(i);
            }
        }

        kita.setStatus(alStat);
        conn = db.checkConnection(conn);
        success = db.deleteStatus(conn, id);

        return success;
    }

    public Boolean addStatus(Status status)
    {
        Boolean success = false;
        ArrayList<Status> alStat = kita.getStatus();

        alStat.add(status);

        kita.setStatus(alStat);

        conn = db.checkConnection(conn);
        success = db.insertStatus(conn, status);
        
        return success;
    }
}
